package com.uyaer.el
{
	import com.uyaer.engine.event.EventManager;
	import com.uyaer.enum.RewardType;
	import com.uyaer.event.EType;
	
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.graphics.SolidColor;
	
	import spark.components.BorderContainer;
	import spark.components.Group;
	
	public class RewardEl extends Group
	{
		private const SIZE:int = 36;
		private var _type:int;
		private var img:Image;
		private var selectCon:BorderContainer;
		public var isControled:Boolean = false; //是否被控制了
		public var isControlDown:Boolean = false;
		
		public var isSingleDown:Boolean = false; //选择单个，且没有多选的时候
		public function RewardEl()
		{
			super();
			
			selectCon = new BorderContainer();
			selectCon.width = SIZE;
			selectCon.height = SIZE;
			selectCon.x = -SIZE/2;
			selectCon.y = -SIZE/2;
			selectCon.backgroundFill = new SolidColor(0x0000ff,0.3);
			addElement(selectCon);
			
			img = new Image();
			img.x = -SIZE/2;
			img.y = -SIZE/2;
			
			setSelect(false);
			this.addElement(img);
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUP);
		}
		
		private function onMouseDown(e:MouseEvent):void{
			if(isControled){
				isControlDown = true;
			}else{
				isControlDown = true;
				isSingleDown = true;
				if(e.controlKey){
					EventManager.dispatchEvent(EType.SELECT_MULTI_REWARD,this);
				}else{
					EventManager.dispatchEvent(EType.SELECT_SINGLE_REWARD,this);
				}
			}
		}
		private function onMouseUP(e:MouseEvent):void{
			isControlDown = false;				
			isSingleDown = false;
			
		}
		
		public function setSelect(b:Boolean):void{
			if(b){
				selectCon.visible = true;
			}else{
				selectCon.visible = false;
			}
		}
		
		public function get type():int
		{
			return _type;
		}

		public function set type(value:int):void
		{
			if(type!=value){
				_type = value;
				switch(type)
				{
					case RewardType.STAR:
						img.source = "assets/star.png";						
						break;
					case RewardType.GOLD:
						img.source = "assets/gold.png";						
						break;
					case RewardType.APPLE:
						img.source = "assets/apple.png";						
						break;
					case RewardType.BANANA:
						img.source = "assets/banana.png";						
						break;
					case RewardType.ORANGE:
						img.source = "assets/orange.png";						
						break;
					default:
						throw "no type!!!";
						break;
				}
			}
		}

	}
}